const { addVideo, getVideoById, getVideoByName, getNewVideo, getVideoByCategory } = require("../services/VideoService");

class VideoControll {

  // 创建用户头像
  async addVideo (ctx) {
    const video = ctx.request.body;
    const count = await getVideoByName(video.name);
    if (count) {
      ctx.body = {
        code: 10001,
        msg: '该视频已存在',
      }
      return;
    }
    const result = await addVideo(video)
    if (result) {
      ctx.body = {
        code: 200,
        msg: '添加视频成功'
      }
    } else {
      ctx.body = {
        code: 10001,
        msg: '添加视频失败'
      }
    }
  }

  async getVideoById (ctx) {
    const { id } = ctx.request.query
    const count = await getVideoById(id)
    ctx.body = {
      code: 200,
      msg: '获取视频成功',
      data: count
    }
  }

  async getNewVideo (ctx) {
    const { pageNo, pageSize } = ctx.request.query
    const result = await getNewVideo((pageNo - 1) * pageSize, pageSize)
    ctx.body = {
      code: 200,
      msg: '获取最新视频成功',
      data: result
    }
  }

  async getVideoByCategory (ctx) {
    const { category, pageNo, pageSize } = ctx.request.query
    const result = await getVideoByCategory(category, (pageNo - 1) * pageSize, pageSize)
    ctx.body = {
      code: 200,
      msg: '获取视频成功',
      data: result
    }
  }
}

module.exports = new VideoControll()